package com.member.mp.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.member.common.core.domain.R;
import com.member.common.core.page.PageResult;
import com.member.common.core.page.TableDataInfo;
import com.member.common.enums.CollectionTypeEnum;
import com.member.common.enums.EnableStatusEnum;
import com.member.common.utils.StringUtils;
import com.member.mp.web.user.SupperController;
import com.member.shop.domain.SysBizMaterial;
import com.member.shop.domain.SysBizMaterialCategory;
import com.member.shop.domain.SysBizMessage;
import com.member.shop.domain.SysBizRecharge;
import com.member.shop.service.ISysBizMaterialCategoryService;
import com.member.shop.service.ISysBizMaterialService;
import com.member.shop.service.ISysBizProfileService;
import com.member.shop.service.ISysBizUserCollectService;
import com.member.shop.vo.MaterialVo;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 【素材中心】控制器
 *
 * @author wangxin
 * @version 1.0
 * @date Mon Jul 03 22:06:51 CST 2024
 **/
@RestController
@RequestMapping("/wx/material")
@Slf4j
@Api(value = "素材接口", tags = {"素材接口"})

public class MaterialController extends SupperController {

    @Resource
    private ISysBizMaterialCategoryService sysBizMaterialCategoryService;

    @Resource
    private ISysBizMaterialService sysBizMaterialService;

    @Resource
    private ISysBizUserCollectService sysBizUserCollectService;
    @Resource
    private ISysBizProfileService sysBizProfileService;


    /**
     * 获取素材中心分类
     *
     * @return 分类列表
     */
    @GetMapping("/queryMaterialCategory")
    public R<List<SysBizMaterialCategory>> queryMaterialCategory() {
        try {
            LambdaQueryWrapper<SysBizMaterialCategory> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SysBizMaterialCategory::getTenantId, getTenantId());
            // 根据ID降序查询
            queryWrapper.orderByDesc(SysBizMaterialCategory::getId);
            List<SysBizMaterialCategory> list = sysBizMaterialCategoryService.list(queryWrapper);
            // 返回数据
            return R.ok(list);
        } catch (Exception e) {
            // 异常处理，日志记录，返回友好的错误信息
            log.error("查询素材中心分类失败", e);
            return R.fail("查询素材中心分类失败，请稍后尝试");
        }
    }

    /**
     * 获取素材(带分页)
     *
     * @param searchValue 搜索值
     * @param categoryId  分类ID,全部不用传
     * @return 分类列表
     */
    @GetMapping("/queryMaterial")
    public R<PageResult<MaterialVo>> queryMaterial(@RequestParam(required = false, value = "searchValue") String searchValue,
                                                   @RequestParam(required = false, value = "categoryId") Long categoryId) {
        try {
            startPage();
            LambdaQueryWrapper<SysBizMaterial> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SysBizMaterial::getTenantId, getTenantId());
            queryWrapper.eq(SysBizMaterial::getStatus, EnableStatusEnum.ENABLE.getKey());
            queryWrapper.like(StringUtils.isNotBlank(searchValue), SysBizMaterial::getTitle, searchValue);
            queryWrapper.eq(categoryId != null, SysBizMaterial::getCategoryId, categoryId);
            queryWrapper.orderByDesc(SysBizMaterial::getSort);
            queryWrapper.orderByDesc(SysBizMaterial::getCreateTime);
            List<SysBizMaterial> sysBizMaterials = sysBizMaterialService.list(queryWrapper);
            TableDataInfo dataTable = getDataTable(sysBizMaterials);
            List<?> rows = dataTable.getRows();
            ArrayList<MaterialVo> item = new ArrayList<>();
            for (Object row : rows) {
                SysBizMaterial sysBizMaterial = (SysBizMaterial) row;
                MaterialVo target = new MaterialVo();
                BeanUtils.copyProperties(sysBizMaterial, target);
                target.setIsCollection(sysBizProfileService.isCollected(sysBizMaterial.getId(), getUserId(), CollectionTypeEnum.MATERIAL.getKey()));
                item.add(target);
            }
            return R.ok(new PageResult<>(dataTable.getTotal(), item));
        } catch (Exception e) {
            // 异常处理，日志记录，返回友好的错误信息
            log.error("查询素材中心分类失败", e);
            return R.fail("查询素材中心分类失败，请稍后尝试");
        }
    }

    /**
     * 获取素材详情
     *
     * @param id 素材id
     * @return 素材详情
     */
    @GetMapping("/queryMaterialById/{id}")
    public R<MaterialVo> queryMaterialById(@PathVariable Long id) {
        try {
            SysBizMaterial sysBizMaterial = sysBizMaterialService.getById(id);
            MaterialVo target = new MaterialVo();
            BeanUtils.copyProperties(sysBizMaterial, target);
            return R.ok(target);
        } catch (Exception e) {
            // 异常处理，日志记录，返回友好的错误信息
            log.error("查询素材中心详情失败", e);
            return R.fail("查询素材中心详情失败，请稍后尝试");
        }
    }

}
